package com.rc.saas.tenant.service.hlht;

import com.rc.saas.tenant.mapper.elec.ElecStationMapper;
import com.rc.saas.tenant.mapper.hlht.HlhtTenantOpenStationMapper;
import com.rc.saas.tenant.model.elec.ElecStation;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpen;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenPrice;
import com.rc.saas.tenant.model.hlht.HlhtTenantOpenStation;
import com.rc.saas.tenant.model.operator.TenantOperatorStation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class HlhtTenantOpenStationServiceImpl implements HlhtTenantOpenStationService {
    @Autowired
    private HlhtTenantOpenStationMapper hlhtTenantOpenStationMapper;

    @Autowired
    private ElecStationMapper elecStationMapper;

    @Override
    public HlhtTenantOpenStationMapper getMapper() {
        return hlhtTenantOpenStationMapper;
    }


    @Override
    public void insertByBatch(List<ElecStation> elecStationList, HlhtTenantOpen hlhtTenantOpen, String hlhtOpenforType, String loginName) {
        //先删除旧依赖关系
        //hlhtTenantOpenStationMapper.deleteByBatchHlhtTenantOpen(elecStationList, hlhtTenantOpen, hlhtOpenforType);
        //新增新依赖关系
        hlhtTenantOpenStationMapper.insertByBatch(elecStationList, hlhtTenantOpen, hlhtOpenforType, loginName);
    }

    @Override
    public void deleteByBatchHlhtOperateIds(List<Long> openStationIdList, List<String> hlhtOperateIds) {
        List<Long> elecStationIds = new ArrayList<>();
        for (Long hlhtStationId : openStationIdList) {
            HlhtTenantOpenStation hlhtTenantOpenStation = hlhtTenantOpenStationMapper.selectByPrimaryKey(hlhtStationId);
            if (hlhtTenantOpenStation != null) {
                elecStationIds.add(hlhtTenantOpenStation.getElecStationId());
            }
        }

        //批量删除第三方运营商指定的站点
        for (String hlhtOperateId : hlhtOperateIds) {
            hlhtTenantOpenStationMapper.deleteByBatchHlhtOperateIds(elecStationIds, hlhtOperateId);
        }

    }

    @Override
    public void deleteByBatch(List<Long> openStationIdList) {
        hlhtTenantOpenStationMapper.deleteByBatch(openStationIdList);
    }

    @Override
    public void deleteByBatchHlhtTenantOpen(List<Long> elecStationIds, HlhtTenantOpen hlhtTenantOpen, String hlhtOpenforType) {
        if (elecStationIds.size() == 0) {
            return;
        }
        List<ElecStation> elecStationList = new ArrayList<>();
        for (Long elecStaionId : elecStationIds) {
            ElecStation elecStation = elecStationMapper.selectByPrimaryKey(elecStaionId);
            elecStationList.add(elecStation);
        }
        hlhtTenantOpenStationMapper.deleteByBatchHlhtTenantOpen(elecStationList, hlhtTenantOpen, hlhtOpenforType);
    }

    @Override
    public void updateOperateStatusByBatch(List<Long> openStationIdList, String operateStatus, String loginName) {
        hlhtTenantOpenStationMapper.updateOperateStatusByBatch(openStationIdList, operateStatus, loginName);
    }

    @Override
    public void updateTenantOperatorByStationId(List<TenantOperatorStation> tenantOperatorStationList) {
        hlhtTenantOpenStationMapper.updateTenantOperatorByStationId(tenantOperatorStationList);
    }

    @Override
    public List<HlhtTenantOpenStation> listStationByEnterprisePolicy(String tenantCode, String hlhtOperatorId, String hlhtTenantCode) {
        return hlhtTenantOpenStationMapper.listStationByEnterprisePolicy(tenantCode, hlhtOperatorId, hlhtTenantCode);
    }

    @Override
    public List<Long> getElecStationIdByOpenStation(List<Long> openStationIdList) {
        return hlhtTenantOpenStationMapper.getElecStationIdByOpenStation(openStationIdList);
    }

    @Override
    public void updateHlhtOpenforTypeBatchByOpenStationIdList(List<Long> openStationIdList, String hlhtOpenforType) {
        hlhtTenantOpenStationMapper.updateHlhtOpenforTypeBatchByOpenStationIdList(openStationIdList, hlhtOpenforType);
    }

    @Override
    public void updateOperateStatusByOpenStationId(Long openStationId, String operateStatus) {
        hlhtTenantOpenStationMapper.updateOperateStatusByOpenStationId(openStationId, operateStatus);
    }

    @Override
    public void updateOperateStatusByHlhtTenantOpen(HlhtTenantOpen hlhtTenantOpen, String operateStatus) {
        hlhtTenantOpenStationMapper.updateOperateStatusByHlhtTenantOpen(hlhtTenantOpen, operateStatus);
    }

    @Override
    public void updateHlhtOpenforTypeByElecStationListAndHlhtOperatorNotNull(HlhtTenantOpen hlhtTenantOpen, List<Long> elecStationIdList, String hlhtOpenforType) {
        hlhtTenantOpenStationMapper.updateHlhtOpenforTypeByElecStationListAndHlhtOperatorNotNull(hlhtTenantOpen, elecStationIdList, hlhtOpenforType);
    }

    @Override
    public void updateTenantOperatorCodeByStationId(List<Long> stationIdList, String tenantOperatorCode) {
        hlhtTenantOpenStationMapper.updateTenantOperatorCodeByStationId(stationIdList, tenantOperatorCode);
    }

    @Override
    public List<Long> selectOpenStationIdByhlhtTenantOpen(List<Long> stationIdList, HlhtTenantOpen hlhtTenantOpen) {
       return hlhtTenantOpenStationMapper.selectOpenStationIdByhlhtTenantOpen(stationIdList, hlhtTenantOpen);
    }
}
